#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    :  
# @function: 
# @version :

import json
import requests



def get_data(page):
    url = 'https://api.douguo.net/home/notes/60/20'
    headers = {
        # 'sign': 'ashdjsdhjkfhksd',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 7.1.2; SM-G973N Build/PPR1.190810.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/92.0.4515.131 Mobile Safari/537.36'
    }
    data = {
        "client": "4",
        "_session": "1680873317308351564608872123",
        "direction": "2",
        "btmid": "[31538489,31537960]",
        "is_new_user": "0",
        "request_count": page,
        "sign_ran": "456ee7d3708763a92ceedaa997b1f8e0",  # 加密
        "code": "d02bdce1ae10467c"
    }
    response = requests.post(url, headers=headers, data=data)
    return response.json()


def parse_data(data):
    for i in data['result']['list']:
        item = {}
        item['title'] = i['note']['title']
        item['name'] = i['note']['author']['n']
        item['img_url'] = i['note']['image_u']
        print(item)
        save_data(item)


def save_data(item):
    with open('douguo.json', 'a+', encoding="utf-8")as f:
        f.write(json.dumps(item, indent=2, ensure_ascii=False))
        f.write(',\n')


def main():
    for i in range(1, 50):
        data = get_data(i)
        parse_data(data)


if __name__ == '__main__':
    main()
